package com.fresh.bus.chat.business;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.canoe.session.SessionRoleManager;
import com.fresh.bus.chat.ctrl.OpCodes;
import com.fresh.bus.chat.domain.AssoLog;
import com.fresh.bus.chat.domain.TreeLog;
import com.fresh.bus.chat.persist.UserDao;
import com.fresh.bus.chat.util.DateUtil;

@Component
public class AssoValueService extends BaseLogService {

	@Resource
	protected SessionRoleManager roleManager;
	@Resource
	private UserDao dao;
	@Resource
	private SystemConfig config;

	private static final Logger log = LoggerFactory
			.getLogger(AssoValueService.class);
	private StringRedisTemplate template;

	@Autowired
	public AssoValueService(StringRedisTemplate template) {
		this.template = template;
	}

	public boolean update(String from, String to, int actionType, String msgKey) {
		String uid = KeyUtils.asso(from, to);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		int result = 0;
		if (ops.size() > 0) {
			int assValue = Integer.valueOf(ops.get("assoValue"));
			assValue += getAssoValueByAction(actionType);
			ops.put("assoValue", String.valueOf(assValue));
			//this.validAvailUpgrade(from, to);
			assValue = Integer.valueOf(ops.get("assoValue"));
			String eventLog = ops.get("eventLog");
			eventLog = appendLog(eventLog, from, actionType, msgKey);
			ops.put("eventLog", eventLog);

			int level = Integer.valueOf(ops.get("level"));
			result = dao.updateAsso(Integer.valueOf(from),
					Integer.valueOf(to), assValue, level, eventLog);
			
			Session session = this.roleManager.getSession(from);
			Response response = new Response(OpCodes.SYN_FRIEND);
			response.setData(ops.entries());
			if (session != null)
				session.send(response);			
		}
		return result > 0;
	}

	protected String appendLog(String srcLog, String who, int actionType,
			String msgKey) {
		AssoLog log = new AssoLog();
		String now = DateUtil.convertDateToString(new Date());
		log.setD(now);
		log.setU(who);
		log.setA(actionType);
		log.setT(msgKey);
		srcLog = appendLog(srcLog, log);
		return srcLog;
	}

	public int getAssoValueByAction(int action) {
		int v = 0;
		switch (action) {
		case AssoLog.FOLLOW_USER:
			v = 15;
			break;		
		case AssoLog.SAY_HELLO:
			v = 2;
			break;
		case AssoLog.PRESENT_WATER:
			v = 4;
			break;
		case AssoLog.ADD_WORDS:
			v = 2;
			break;
		case AssoLog.ONLINE_TALK:
			v = 3;
			break;
		default:
			break;
		}
		return v;
	}

	public int getUpgradeExp(int level) {
		int l = level + 1 > 6 ? 6 : (level + 1);
		Map<String, Object> cond = config.findAsso(l);
		int needExp = (Integer) cond.get("exp");
		return needExp;
	}

	public void validAvailUpgrade(String from, String to) {
		Session session = this.roleManager.getSession(from);
		String uid = KeyUtils.asso(from, to);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		int level = Integer.valueOf(ops.get("level"));
		int exp = Integer.valueOf(ops.get("exp"));
		int upgradeExp = getUpgradeExp(level);

		if (exp >= upgradeExp) {
			ops.put("assValue", String.valueOf(upgradeExp));
			Response response = new Response(OpCodes.AVAIL_ASSO_UPGREADE);
			response.setData(true);
			if (session != null)
				session.send(response);
		}
	}
}
